function Pointer(x, y) {
	this.x = x;
	this.y = y;
}

function Position(left, top) { //坐标
	this.left = left;
	this.top = top;
}

function Direction(horizontal, vertical) { //方向
	this.horizontal = horizontal;
	this.vertical = vertical;
}

function initSelectDashed(els){
	var impactEls = els;//要碰撞檢測的元素
	var oldPointer = new Pointer(); //起点
	var oldPosition = new Position(); //位置
	var direction = new Direction(); //方法
	var div = $("<div/>").css({
		background: "blue",
		position: "absolute",
		opacity: "0.2"
	}).appendTo($("body"));
	var isDown = false;
	$(document).mousedown(function(e) {
		e.preventDefault(); //去掉默认事件
		if(div[0].setCapture) {
			div[0].setCapture(true) ;
		}
		isDown = true;
		oldPointer.x = e.clientX;
		oldPointer.y = e.clientY;
		oldPosition.left = e.clientX;
		oldPosition.top = e.clientY;
		div.css({
			left: e.clientX,
			top: e.clientY
		});
	});
	div.extend({
		checkImpact: function(tds) {
			var $this = $(this);
			tds.each(function() {
				if ($this.offset().left + $this.width() > $(this).offset().left && $this.offset().left < $(this).offset().left + $(this).width() && $this.offset().top + $this.height() > $(this).offset().top && $this.offset().top < $(this).offset().top + $(this).height()) { //碰撞检测
					$(this).addClass('active');
					$current_table = $(this).parents('table.floor-table');
				} else {
					$(this).removeClass('active');
				}
			});
		}
	});
	$(document).mousemove(function(e) {
		if (!isDown) return isDown;
		if (e.clientX > oldPointer.x) { //右方向
			direction.horizontal = "Right";
		} else if (e.clientX < oldPointer.x) { //左方向
			direction.horizontal = "Left";
		} else {
			direction.horizontal = "";
		}
		if (e.clientY > oldPointer.y) {
			direction.vertical = "Down";
		} else if (e.clientY < oldPointer.y) {
			direction.vertical = "Up";
		} else {
			direction.vertical = "";
		}
		var directionOperation = {
			LeftUp: function() {
				div.css({
					width: Math.abs(e.clientX - oldPointer.x),
					height: Math.abs(e.clientY - oldPointer.y),
					top: oldPosition.top - Math.abs(e.clientY - oldPointer.y),
					left: oldPosition.left - Math.abs(e.clientX - oldPointer.x)
				});
			},
			LeftDown: function() {
				div.css({
					width: Math.abs(e.clientX - oldPointer.x),
					height: Math.abs(e.clientY - oldPointer.y),
					left: oldPosition.left - Math.abs(e.clientX - oldPointer.x)
				});
			},
			Down: function() {
				div.css({
					height: Math.abs(e.clientY - oldPointer.y)
				});
			},
			Up: function() {
				div.css({
					height: Math.abs(e.clientY - oldPointer.y),
					top: oldPosition.top - Math.abs(e.clientY - oldPointer.y)
				});
			},
			Right: function() {
				div.css({
					width: Math.abs(e.clientX - oldPointer.x),
				});
			},
			Left: function() {
				div.css({
					width: Math.abs(e.clientX - oldPointer.x),
					left: oldPosition.left - Math.abs(e.clientX - oldPointer.x)
				});
			},
			RightDown: function() {
				div.css({
					width: Math.abs(e.clientX - oldPointer.x),
					height: Math.abs(e.clientY - oldPointer.y)
				});
			},
			RightUp: function() {
				div.css({
					width: Math.abs(e.clientX - oldPointer.x),
					height: Math.abs(e.clientY - oldPointer.y),
					top: oldPosition.top - Math.abs(e.clientY - oldPointer.y)
				});
			}
		}
		if (direction.horizontal && direction.vertical) {
			directionOperation[(direction.horizontal + direction.vertical)]();
			div.checkImpact(impactEls);//碰撞檢測
		}
	});
	$(document).mouseup(function() {
		if (!isDown) return isDown;
		isDown = false;
		div.width(0).height(0);
		if(div[0].releaseCapture) {
			div[0].releaseCapture(true) ;
		}
	});
}